package com.tianpengtech.front.mobile.publisher;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.tianpengtech.common.model.authenticate.Authenticate;
import com.tianpengtech.common.model.fair.Fair;
import com.tianpengtech.common.model.fairmessage.FairMessage;
import com.tianpengtech.common.model.fairreport.FairReport;
import com.tianpengtech.common.model.task.Task;
import com.tianpengtech.common.model.task.TaskWh;
import com.tianpengtech.common.model.user.User;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.web.passport.PassportService;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

/**
 * Created by ggd on 2017/8/3 0003.
 */
public class MobilePublishService {
    public static MobilePublishService me = new MobilePublishService();
    private static Authenticate authDao =  Authenticate.dao;
    private static Task taskDao =  Task.dao;
    private static PassportService ps = PassportService.me;
    /**
     * 获取商家详情信息
     * @param id
     * @return
     */
    public R getPublish(String id){
        String sql = String.format("select id,company_name,user_id,status,company_info from %s where %s=? and %s=? and %s=?",authDao.tableName(),Authenticate.DELETETAG,Authenticate.STATUS,Authenticate.USER_ID);
        Authenticate auth=authDao.findFirst(sql,Authenticate.DELETE_STATUS_NO,Authenticate.STATUS_SUCCESS,id);
        if (auth==null){
            return R.fail("用户不存在");
        }
        R r=R.ok();
        r.p("auth",auth);
        String taskSql=String.format("select sum(wh.click_num) as click,SUM(wh.cvs_num) as cvs from %s wh left join %s task on task.id=wh.task_id where task.%s=?", TaskWh.dao.tableName(),taskDao.tableName(),Task.CREATE_USER);
        Task task=taskDao.findFirst(taskSql,id);
        if (task==null){
            r.p("click",0);
            r.p("cvs","0.00%");
        }
        BigDecimal click=task.get("click");
        BigDecimal cvs=task.get("cvs");
        r.p("click",click.intValue());
        double d=0;
        if(cvs!=null){
            d=cvs.doubleValue()/click.doubleValue();
        }
        DecimalFormat df=(DecimalFormat) DecimalFormat.getInstance();
        df.applyPattern("##.##%");
        r.p("cvs",df.format(d));
        return r;
    }

    /**
     * 获取商家发布的任务
     * @param id
     * @return
     */
    public R getPublishTask(int pageNum, int length,String id){
        String select="select id,task_title,status";
        String sql = String.format(" from %s where %s=? and %s=? and %s>=? order by %s desc",taskDao.tableName(),Task.DELETETAG,Task.CREATE_USER,Task.STATUS,Task.CREATE_TIME);
        Page<Task> page=taskDao.paginate(pageNum,length,select,sql,Task.DELETE_STATUS_NO,id,Task.STATUS_SUCCESS);
        R r = R.ok("成功");
        r.p("count",page.getTotalRow());
        r.p("pageNum",page.getPageNumber());
        r.p("list",page.getList());
        return r;
    }


    /**
     * 获取商家的评论
     * @param pageNum
     * @param length
     * @param id
     * @return
     */
    public R getPublishComment(int pageNum, int length,String id){
        String select="select wh.evaluate_whom,wh.whom_date,fwh.nick_name,fwh.photo,fwh.id";
        String sql = String.format("from %s wh left join %s task on task.id=wh.task_id  LEFT JOIN %s fwh on wh.wh_id = fwh.id where task.create_user=? and wh.evaluate_whom is not null order by wh.%s desc",TaskWh.dao.tableName(),taskDao.tableName(), Fair.dao.tableName(), TaskWh.WHOM_DATE);
        Page<Task> page=taskDao.paginate(pageNum,length,select,sql,id);
        R r = R.ok("成功");
        r.p("count",page.getTotalRow());
        r.p("pageNum",page.getPageNumber());
        r.p("list",page.getList());
        return r;
    }


    /**
     * 保存举报信息
     * @param contentId 数据字典code
     * @param content 其他：举报信息
     * @param uid 举报人ID
     * @param userId 被举报人ID
     * @return
     */
    public R saveReport(String contentId,String content,long uid,String userId){
        if (!ps.isUserIdExists(userId,User.USER_TYPE_PROV)){
            return R.fail("被举报人不存在");
        }
        FairReport fr = new FairReport();
        fr.set(FairReport.FAIR_ID,uid);
        fr.set(FairReport.USER_ID,userId);
        if (StrKit.isBlank(contentId)){
            fr.set(FairReport.CONTENT,content);
        }else{
            fr.set(FairReport.CONTENT_ID,contentId);
        }
        if (fr.save()){
            return R.ok();
        }else{
            return R.fail("保存失败");
        }
    }


}
